package org.darcy.youchat.activity.home;

import android.content.Intent;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.netease.nimlib.sdk.uinfo.constant.GenderEnum;
import com.netease.nimlib.sdk.uinfo.model.NimUserInfo;

import org.darcy.youchat.R;
import org.darcy.youchat.activity.BaseActivity;
import org.darcy.youchat.databinding.ActivityAddFriendBinding;
import org.darcy.youchat.factory.ThreadPoolFactory;
import org.darcy.youchat.imageloader.MyImageLoader;
import org.darcy.youchat.model.UserCache;
import org.darcy.youchat.nimsdk.NimUserInfoSDK;
import org.darcy.youchat.utils.AppConst;
import org.darcy.youchat.utils.UIUtils;
import org.darcy.youchat.view.CustomDialog;

import cn.bingoogolapple.qrcode.zxing.QRCodeEncoder;

/**
 * 添加朋友界面
 */
public class AddFriendActivity extends BaseActivity {

    private ActivityAddFriendBinding binding;

    private Intent mIntent;

    private NimUserInfo mNimUserInfo;
    private View mQRCodeCardView;
    private CustomDialog mQRCodeCardDialog;
    private ImageView mIvHeaderQRCodeCard;
    private TextView mTvNameQRCodeCard;
    private ImageView mIvGenderQRCodeCard;
    private ImageView mIvCardQRCodeCard;


    @Override
    public void init() {
        mNimUserInfo = NimUserInfoSDK.getUser(UserCache.getAccount());
    }

    @Override
    public void initView() {
        binding = ActivityAddFriendBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setSupportActionBar(binding.mToolbar.toolbar);
        String titleName = "添加朋友";
        getSupportActionBar().setTitle(titleName);
        binding.mToolbar.toolbar.setNavigationIcon(R.mipmap.ic_back);
    }

    @Override
    public void initListener() {
        binding.etContent.setOnClickListener(v -> {
            mIntent = new Intent(this, SearchUserActivity.class);
            mIntent.putExtra(SearchUserActivity.SEARCH_TYPE, SearchUserActivity.SEARCH_USER_REMOTE);
            startActivity(mIntent);
        });
        binding.ivQRCordCard.setOnClickListener(v -> {
            if (mQRCodeCardView == null) {
                mQRCodeCardView = View.inflate(AddFriendActivity.this, R.layout.include_qrcode_card, null);
                mQRCodeCardView.setBackgroundResource(R.drawable.shape_corner_rect_solid_white);
                mIvHeaderQRCodeCard = mQRCodeCardView.findViewById(R.id.ivHeader);
                mTvNameQRCodeCard = mQRCodeCardView.findViewById(R.id.tvName);
                mIvGenderQRCodeCard = mQRCodeCardView.findViewById(R.id.ivGender);
                mIvCardQRCodeCard = mQRCodeCardView.findViewById(R.id.ivCard);
                mQRCodeCardDialog = new CustomDialog(AddFriendActivity.this, 300, 400, mQRCodeCardView, R.style.dialog);
            }

            String avatar = mNimUserInfo.getAvatar();
            if (TextUtils.isEmpty(avatar)) {
                mIvHeaderQRCodeCard.setImageResource(R.mipmap.default_header);
            } else {
                MyImageLoader.LoadNetImage(avatar, mIvHeaderQRCodeCard);
            }
            mTvNameQRCodeCard.setText(mNimUserInfo.getName());
            if (mNimUserInfo.getGenderEnum() == GenderEnum.FEMALE) {
                mIvGenderQRCodeCard.setImageResource(R.mipmap.ic_gender_female);
            } else if (mNimUserInfo.getGenderEnum() == GenderEnum.MALE) {
                mIvGenderQRCodeCard.setImageResource(R.mipmap.ic_gender_male);
            } else {
                mIvGenderQRCodeCard.setVisibility(View.GONE);
            }
            showQRCordCard();
            mQRCodeCardDialog.show();
        });

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }


    private void showQRCordCard() {
        ThreadPoolFactory.getNormalPool().execute(() -> {
            final Bitmap codeWithLogo5 = QRCodeEncoder.syncEncodeQRCode(AppConst.QRCodeCommend.ACCOUNT + mNimUserInfo.getAccount(), UIUtils.dip2Px(200));
            UIUtils.postTaskSafely(() -> mIvCardQRCodeCard.setImageBitmap(codeWithLogo5));
        });

    }
}
